package com.strawberry.chat2.views.svgaImage;

import android.content.ContentResolver;
import android.net.Uri;

import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.opensource.svgaplayer.SVGACallback;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;

import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * Created by Administrator on 2019/3/7.
 */

public class SvgaImageManager extends SimpleViewManager<SVGAImageView> {
    private static final String GIFVIEW_MANAGER_NAME = "SvgaImage";
    private static final String PROP_SRC = "source";

    private ThemedReactContext mContext;

    @Override
    public String getName() {
        return GIFVIEW_MANAGER_NAME;
    }

    @Override
    protected SVGAImageView createViewInstance(ThemedReactContext reactContext) {
        this.mContext = reactContext;
        SVGAImageView imageView = new SVGAImageView(reactContext);
        return imageView;
    }

    @ReactProp(name = PROP_SRC)
    public void setSrc(final SVGAImageView imageView, ReadableMap source){
        if(source.hasKey("uri")){
            String url = source.getString("uri");
            ContentResolver resolver = mContext.getContentResolver();
            InputStream inStream = null;
            try {
                inStream=resolver.openInputStream(Uri.parse(url));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            };
            if(inStream != null){
                SVGAParser parser = new SVGAParser(mContext);
                parser.parse(inStream, url, new SVGAParser.ParseCompletion() {
                    @Override
                    public void onError() {
                    }

                    @Override
                    public void onComplete(SVGAVideoEntity svgaVideoEntity) {
                        imageView.setLoops(1);
                        imageView.setClearsAfterStop(true);
                        imageView.setVideoItem(svgaVideoEntity);
                        imageView.startAnimation();
                        imageView.setCallback(new SVGACallback() {
                            @Override
                            public void onPause() {

                            }

                            @Override
                            public void onFinished() {
                                mContext
                                        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                                        .emit("onAnimFinish", null);
                            }

                            @Override
                            public void onRepeat() {

                            }

                            @Override
                            public void onStep(int i, double v) {

                            }
                        });
                    }
                    // ...
                },true);
            }
        }
    }
}
